package com.southminority.ethnic.controller.research;

import com.southminority.ethnic.common.result.PageResult;
import com.southminority.ethnic.common.result.R;
import com.southminority.ethnic.service.research.AwardService;
import com.southminority.ethnic.controller.research.vo.AwardVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 获奖情况前端展示控制器
 * 
 * @author SouthMinority
 */
@RestController
@RequestMapping("/api/research/award")
@Tag(name = "14 - 获奖情况展示API", description = "【前台】科研成果-获奖情况查询与展示相关接口")
@CrossOrigin
public class AwardController {

    private final AwardService awardService;

    public AwardController(AwardService awardService) {
        this.awardService = awardService;
    }

    @GetMapping("/page")
    @Operation(summary = "分页和条件查询已发布的获奖列表", description = "用于前端获奖情况列表页面，支持按年份、级别和分类筛选。")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "查询成功"),
            @ApiResponse(responseCode = "500", description = "查询失败")
    })
    public R<PageResult<AwardVO>> getPublishedAwardPage(
            @Parameter(description = "当前页码", example = "1") @RequestParam(defaultValue = "1") Integer page,
            @Parameter(description = "每页显示数量", example = "10") @RequestParam(defaultValue = "10") Integer size,
            @Parameter(description = "获奖年份", example = "2023") @RequestParam(required = false) Integer year,
            @Parameter(description = "奖项级别", example = "一等奖") @RequestParam(required = false) String level,
            @Parameter(description = "奖项类别", example = "科技成果奖") @RequestParam(required = false) String category) {
        try {
            PageResult<AwardVO> pageResult = awardService.getPublishedAwardPage(page, size, year, level, category);
            return R.ok(pageResult);
        } catch (Exception e) {
            return R.fail("查询获奖列表失败: " + e.getMessage());
        }
    }

    @GetMapping("/detail/{id}")
    @Operation(summary = "获取单个获奖情况的详细信息", description = "根据获奖ID查询，返回其完整的公开信息。")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "获取成功"),
            @ApiResponse(responseCode = "404", description = "获奖记录不存在或未发布"),
            @ApiResponse(responseCode = "500", description = "服务器内部错误")
    })
    public R<AwardVO> getAwardDetail(@Parameter(description = "获奖的唯一ID", example = "1", required = true) @PathVariable Long id) {
        try {
            AwardVO awardVO = awardService.getAwardDetail(id);
            return R.ok(awardVO);
        } catch (Exception e) {
            return R.fail("获取获奖详情失败: " + e.getMessage());
        }
    }

    @GetMapping("/years")
    @Operation(summary = "获取所有获奖年份列表", description = "用于前端筛选菜单，返回一个包含所有不重复年份的列表。")
    @ApiResponses(value = {@ApiResponse(responseCode = "200", description = "获取成功")})
    public R<List<Integer>> getAwardYears() {
        return R.ok(awardService.getAwardYears());
    }

    @GetMapping("/levels")
    @Operation(summary = "获取所有奖项级别列表", description = "用于前端筛选菜单，返回一个包含所有不重复奖项级别的字符串列表。")
    @ApiResponses(value = {@ApiResponse(responseCode = "200", description = "获取成功")})
    public R<List<String>> getAwardLevels() {
        return R.ok(awardService.getAwardLevels());
    }

    @GetMapping("/categories")
    @Operation(summary = "获取所有奖项分类列表", description = "用于前端筛选菜单，返回一个包含所有不重复奖项分类的字符串列表。")
    @ApiResponses(value = {@ApiResponse(responseCode = "200", description = "获取成功")})
    public R<List<String>> getAwardCategories() {
        return R.ok(awardService.getAwardCategories());
    }

    @GetMapping("/test")
    @Operation(summary = "测试接口连通性", description = "一个简单的GET请求，用于检查服务是否正常运行。")
    public R<String> test() {
        return R.ok("获奖情况前端服务正常");
    }
} 
